
//搜索用的数据
var DATA = {

    page : 1,

    per_page : 10,

    machine_id : '',

    store_id : '',

    customer_id : '',

    start_at : '',

    end_at : '',

    pay_status : '',

    process_status : '',

    order_type : '',

    trade_no : '',

    recipient : '',

    first_title : ''

};

//vue的data
var _Global_Data = {

    pay_status_map : {

        '0' : '未支付',

        '1' : '正在支付',

        '2' : '支付成功',

        '10' : '退款中',

        '40' : '已退款'

    },                      //订单状态映射表

    process_status_map : {

        '-1' : '已取消',

        '0' : '待发货',

        '1' : '待收货',

        '2' : '待评价',

        '3' : '已评价',

        '5' : '处理中'

    },                      //货物状态映射表

    order_type_map : {

        null: '满天星一体机',

        vendor: '自动售卖机',

        eshop_wemall: '微商城',

        eshop_pad: '开店宝',

        Eshop: '开店宝',

    },                      //订单来源映射表

    query_info : DATA,      //查询所需要的数据

    search_info : {},       //用于接受查询数据

    subordinates : {},      //本地存储的下级信息

    income : '',            //支付总额

    total_money : '',       //订单总额

    current_order : '',     //当前查看的订单

    is_wemall : false,      //是否是微商城下查看

    machines : [],          //本地存储的机器数组

    order : {},              //订单状态信息

    order_ids : [],          //新订单id集合

    batch_express_code : '',    //批量发货时的快递单号

    batch_express_company_code : '',    //批量发货时的快递公司编号

    checkall : false        //是否选择所有

};

// 订单
var orderResource = new Resource( API_LIST.ADMIN_ORDER_QUERY , Vue.http );

// 物流
var expressResource = new Resource( API_LIST.EXPRESS_UPDATE , Vue.http );

//批量发货
var batchResource = new Resource( API_LIST.BATCH_EXPRESS , Vue.http );

!function orderQuery() {

    $('.select2_category').css('width',100);

    $('.select2_category').eq(0).css('width',150);

    $('.select2_category').eq(1).css('width',150);

    $('.select2_category').eq(2).css('width',150);

    var vm = new Vue({

        el : '#order_list',

        data : _Global_Data,

        created : function(){

            var self = this;

            self.initSearchData()
                .then(self.requestTableData(self.query_info))

        },

        computed : {



        },

        watch : {

            'checkall' : function checkall(){

                var self = this;

                this.order.data.forEach(function(order){

                    order.is_selected = self.checkall;

                })

            }

        },

        methods : {
            //是否显示状态修改
            reviseStatus : function(data){

                if( data.process_status == 0 || data.process_status == 1 || data.process_status == 5 ){

                    return true;

                }else {

                    return false;

                }

            },
            //select2获取value的方法
            getValue : function getSelectValue(e){

                if( e.target.dataset.name == 'express_company_code' ){

                    this.current_order[e.target.dataset.name] = e.target.value;

                }else if( e.target.dataset.name == 'batch_express_company_code' ){

                    this.batch_express_company_code = e.target.value;

                }else {

                    this.query_info[e.target.dataset.name] = e.target.value;

                }

            },
            //清空日期的方法
            resetTime: function resetTime(id) {

                this.$refs[id].value = '';

            },
            //改变页数的时候出发的函数
            pageChange : function pageChange(val){

                this.search_info.page = val;

                this.search_info.per_page = this.query_info.per_page;

                this.search_info.order_type = this.query_info.order_type;

                this.requestTableData(this.search_info);

            },
            //改变每页显示数时触发的函数
            sizeChange : function sizeChange(val){

                this.search_info.per_page = val;

                this.query_info.per_page = val;

                this.requestTableData(this.search_info);

            },
            //点击搜索时触发的函数
            confirmSearch : function confirmSearch(){

                this.query_info.start_at = this.$refs.startTime.value;

                this.query_info.end_at = this.$refs.endTime.value;

                var json_object = JSON.stringify(this.query_info);

                this.search_info = JSON.parse(json_object);

                this.requestTableData(this.query_info);

            },
            //改变订单状态的方法
            orderStatusChange : function orderStatusChange(data){

                var self = this;

                App.blockUI('#order_list_form');

                var param = {

                    process_status : data.process_status

                };

                orderResource.update( data.id, param )
                            .then(function(res){

                                if( parseInt(res.data.code) === 0 ){

                                    self.requestTableData(self.search_info);

                                    alert('修改成功！');

                                }else {

                                    alert(res.data.error.process_status[0]);

                                }

                                App.unblockUI('#order_list_form');

                            })
                            .catch(function(err){

                                console.log(err);

                                App.unblockUI('#order_list_form');

                                alert('网络异常，请重试！');

                            })

            },
            //查看快递信息
            expressDetail : function expressDetail(id){

                var $express_input = $('#express');

                //初始化快递选择下拉框
                for ( var key in EXPRESS_CODE_MAP ){

                    $express_input.append("<option value='"+ key +"'>"+ EXPRESS_CODE_MAP[key] +"</option>")

                }

                $express_input.select2();

                $('#stack1 .select2_category').css('width',200);

                var current_order = this.order.data.filter(function(e){

                    return e.id == id;

                })[0];

                this.current_order = current_order;

                $express_input.val(this.current_order.express_company_code).trigger('change');

            },
            //批量发货
            batchExpress : function batch_express(){

                var $express_input = $('#batch_express');

                //初始化快递选择下拉框
                for ( var key in EXPRESS_CODE_MAP ){

                    $express_input.append("<option value='"+ key +"'>"+ EXPRESS_CODE_MAP[key] +"</option>")

                }

                $express_input.select2();

                $('#stack2 .select2_category').css('width',200);

            },
            //批量填写快递信息
            batchPostExpress : function batchPostExpress(){

                var order_ids = [];

                var self = this;

                App.blockUI('#batch_form');

                this.order.data.forEach(function(order){

                    if( order.is_selected ){

                        order_ids.push(order.id);

                    }

                });

                var params = {

                    exp_code : self.batch_express_company_code,

                    exp_no : self.batch_express_code,

                    order_ids : order_ids

                };

                batchResource.store(params)
                            .then(function (res) {

                                if (parseInt(res.data.code) === 0) {

                                    alert('发货成功！');

                                    $('.close').trigger('click');

                                    self.requestTableData(self.query_info);

                                } else {

                                    alert(res.data.error);

                                }

                                App.unblockUI('#batch_form');

                            })
                            .catch(function (err) {

                                console.log(err);

                                App.unblockUI('#batch_form');

                                alert('网络异常！');

                            })

            },
            //修改快递信息
            expressUpdate : function expressUpdate(){

                var self = this;

                App.blockUI('#express_form');

                var params = {

                    exp_code : self.current_order.express_company_code,

                    exp_no : self.current_order.expressage_code

                };

                expressResource.update( self.current_order.id , params )
                    .then(function(res){

                        if( parseInt(res.data.code) === 0 ){

                            $('.close').trigger('click');

                            if( self.current_order.process_status == 0 ) {

                                self.current_order.process_status = 1;

                                self.orderStatusChange(self.current_order);

                            }

                        }else if( parseInt(res.data.code) === 4006 ){

                            alert('缺少参数！');

                        } else {

                            alert(res.data.error);

                        }

                        App.unblockUI('#express_form');

                    })
                    .catch(function(err){

                        console.log(err);

                        App.unblockUI('#express_form');

                        alert('网络异常，请重试！');

                    })
            },
            //查看订单详情
            orderDetail : function orderDetail(id){

                window.open( API_LIST.ORDER_URL + id );

            },
            //判断网址的函数
            urlSplit : function urlSplit(url){

                return url.split('management/')[1];

            },
            //搜索函数
            requestTableData : function requestTableData( params ){

                var self = this;

                App.blockUI('#order_list_form');

                orderResource.index( params )
                            .then(function(res){

                                if( parseInt(res.data.code) === 0 ){

                                    res.data.orders.data.forEach(function(data){

                                        data.is_selected = false;

                                    });

                                    self.order = res.data.orders;

                                    self.order.per_page = parseInt(self.order.per_page);

                                    self.income = res.data.finance_info.income;

                                    self.total_money = res.data.finance_info.gross;

                                    _HEAD_GLOBAL_DATA.new_order_infos = [];

                                }else {

                                    alert(res.data.error);

                                }

                                App.unblockUI('#order_list_form');

                            })
                            .catch(function(err){

                                console.log(err);

                                App.unblockUI('#order_list_form');

                                alert('网络异常，请重试！');

                            })

            },
            //初始化所需数据
            initSearchData : function initSearchData(){

                var self = this;

                if( self.urlSplit(location.href) == 'wemall-order-query' ){

                    self.query_info.order_type = 'eshop_wemall';

                    this.is_wemall = true;

                }

                if( _HEAD_GLOBAL_DATA.new_order_infos.length != 0 ){

                    self.order_ids = head_vm.data.new_order_ids;

                    head_vm.data.new_order_infos = [];

                }

                return orderResource.create()
                                    .then(function(res){

                                        if( parseInt(res.data.code) === 0 ){

                                            self.subordinates = res.data.subordinates;

                                            self.machines = res.data.machines;

                                        }else {

                                            alert(res.data.error);

                                        }

                                    })
                                    .catch(function(err){

                                        console.log(err);

                                        alert('网络异常，请重试！');

                                    })

            }

        }

    })

}();